﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Widgetsphere.AutoAggregate.Business.Objects;
using Widgetsphere.AutoAggregate.CeleriqRepository.DataService;
using Widgetsphere.AutoAggregate.CeleriqRepository;

namespace Widgetsphere.AutoAggregate.Website.Objects
{
	public class RepositoryMaintenance
	{
		public void DeleteIndex(Listing listing)
		{
			var repositoryHelper = AutoRepositoryHelper.GetInstance();
			repositoryHelper.DeleteIndex(listing.ListingId);
		}

		public void UpdateIndex(ListingCollection listingCollection)
		{
			var repositoryHelper = AutoRepositoryHelper.GetInstance();

			//Delete non-active jobs
			foreach (Listing item in listingCollection)
			{
				if (!item.Active)
				{
					repositoryHelper.DataService.DeleteData(repositoryHelper.AccessToken, item.ListingId);
				}
			}

			//Add all active jobs
			List<AutoItem> list = new List<AutoItem>();
			foreach (Listing listing in listingCollection)
			{
				if (listing.Active) list.Add(LoadCQItem(listing));

				//Delete 50 at a time
				if (list.Count >= 50)
				{
					repositoryHelper.UpdateIndex(list);
					list.Clear();
				}
			}

			//Send jobs to index
			if (list.Count > 0)
				repositoryHelper.UpdateIndex(list);

		}

		public void UpdateIndex(Listing listing)
		{
			try
			{
				var repositoryHelper = AutoRepositoryHelper.GetInstance();
				if (!listing.Active) return;
				AutoItem newItem = LoadCQItem(listing);
				repositoryHelper.DataService.AddData(repositoryHelper.AccessToken, newItem);
				repositoryHelper.DataService.Summarize(repositoryHelper.AccessToken);
			}
			catch (Exception ex)
			{
				//AccountHelper.SendErrorMail(ex);
			}
		}

		private AutoItem LoadCQItem(Listing listing)
		{
			AutoItem newItem = new AutoItem();

			//List<string> bulletList = new List<string>();
			//foreach (JobBullet jobBullet in listing.JobBulletList)
			//{
			//  bulletList.Add(jobBullet.Text);
			//}

			//newItem.Bullets = bulletList.ToArray();
			//if ((listing.City != null) && (listing.City != "")) newItem.City = listing.City;
			//newItem.Company = listing.FacilityItem.CompanyItem.Name;
			//newItem.Description = listing.Description;
			//newItem.EmployerID = listing.UserId;
			//newItem.EmployerType = listing.FacilityItem.UserTypeItem.Name;
			//newItem.ExpirationDate = listing.ExpirationDate;
			//newItem.IsAgency = (listing.FacilityItem.UserTypeId == (int)UserTypeConstants.Agency);
			//newItem.JobID = listing.JobId;
			//newItem.ImagePath = listing.Imagepath;
			//if (listing.JobDurationTypeItem != null)
			//  newItem.JobType = listing.JobDurationTypeItem.Name;

			//newItem.Location = null;
			//if ((listing.Latitude != null) && (listing.Longitude != null))
			//{
			//  newItem.Location = new GeoCode();
			//  newItem.Location.Latitude = listing.Latitude.Value;
			//  newItem.Location.Longitude = listing.Longitude.Value;
			//}

			//newItem.MaxSalary = null;
			//newItem.PostedDate = listing.ModifiedDate;
			//newItem.Priority = listing.FacilityItem.Priority;
			//if (listing.SpecialtyItem != null)
			//{
			//  newItem.Specialty = listing.SpecialtyItem.Name;
			//  newItem.SpecialtyId = listing.SpecialtyId;
			//}
			//if ((listing.StateLong != null) && (listing.StateLong != "")) newItem.State = listing.StateLong;
			//if ((listing.Title != null) && (listing.Title != "")) newItem.Title = listing.Title;
			//newItem.Zip = listing.Zip;

			////Premium Stuff
			//if (listing.JobPremiumList.Count > 0)
			//{
			//  JobPremium jobPremium = listing.JobPremiumList[0];
			//  newItem.PremiumExpirationDate = jobPremium.ExpirationDate;
			//  newItem.PremiumIsActive = jobPremium.Active;
			//  newItem.PremiumJobId = jobPremium.JobId;
			//  newItem.PremiumJobIsGeo = (jobPremium.State != null);
			//  newItem.PremiumJobIsHomepage = (jobPremium.JobPremiumTypeId == (int)JobPremiumTypeConstants.HomePage);
			//  if (jobPremium.SpecialtyId != null) newItem.PremiumSpecialty = jobPremium.SpecialtyItem.Name;
			//  newItem.PremiumStartDate = jobPremium.StartDate;
			//  newItem.PremiumState = jobPremium.State;
			//}

			return newItem;

		}

	}
}